package com.uls.web.controller.lh;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.uls.lh.domain.LhCarinfo;
import com.uls.lh.domain.LhDriverinfo;
import com.uls.lh.service.ILhCarinfoService;
import com.uls.lh.service.ILhDriverinfoService;
import com.uls.lh.vo.ReportBarchartVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.lh.domain.LhCarrequest;
import com.uls.lh.service.ILhCarrequestService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 用车申请管理Controller
 *
 * @author lh
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/lh/carrequest")
public class LhCarrequestController extends BaseController {
    @Autowired
    private ILhCarrequestService lhCarrequestService;
    @Autowired
    private ILhCarinfoService lhCarinfoService;
    @Autowired
    private ILhDriverinfoService lhDriverinfoService;


    /**
     * 查询用车申请管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhCarrequest lhCarrequest) {
        startPage();
        List<LhCarrequest> list = lhCarrequestService.selectLhCarrequestList(lhCarrequest);
        for (LhCarrequest lhCarrequest1 : list) {
            Long carId = lhCarrequest1.getCarId();
            Long driverId = lhCarrequest1.getDriverId();
            LhCarinfo lhCarinfo = lhCarinfoService.selectLhCarinfoByCarId(carId);
            LhDriverinfo lhDriverinfo = lhDriverinfoService.selectLhDriverinfoByDriverId(driverId);
            if (lhCarinfo != null) {
                lhCarrequest1.setModel(lhCarinfo.getModel());
            }
            if (lhDriverinfo != null)
            {
                lhCarrequest1.setName(lhDriverinfo.getName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用车申请管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:export')")
    @Log(title = "用车申请管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LhCarrequest lhCarrequest) {
        List<LhCarrequest> list = lhCarrequestService.selectLhCarrequestList(lhCarrequest);
        ExcelUtil<LhCarrequest> util = new ExcelUtil<LhCarrequest>(LhCarrequest.class);
        util.exportExcel(response, list, "用车申请管理数据");
    }

    /**
     * 获取用车申请管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:query')")
    @GetMapping(value = "/{requestId}")
    public AjaxResult getInfo(@PathVariable("requestId") Long requestId) {
        return success(lhCarrequestService.selectLhCarrequestByRequestId(requestId));
    }

    /**
     * 新增用车申请管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:add')")
    @Log(title = "用车申请管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LhCarrequest lhCarrequest) {
        return toAjax(lhCarrequestService.insertLhCarrequest(lhCarrequest));
    }

    /**
     * 修改用车申请管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:edit')")
    @Log(title = "用车申请管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LhCarrequest lhCarrequest) {
        return toAjax(lhCarrequestService.updateLhCarrequest(lhCarrequest));
    }

    /**
     * 删除用车申请管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:remove')")
    @Log(title = "用车申请管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{requestIds}")
    public AjaxResult remove(@PathVariable Long[] requestIds) {
        return toAjax(lhCarrequestService.deleteLhCarrequestByRequestIds(requestIds));
    }


    /**
     * 提交用车申请
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:carrequestSubmit')")
    @Log(title = "用车申请", businessType = BusinessType.EXPORT)
    @GetMapping("/submitlhcarrequest/{requestId}")
    public AjaxResult submitlhcarrequest(@PathVariable Long requestId) {
        return toAjax(lhCarrequestService.submit(requestId, getUserId()));
    }


    /**
     * 查询未审批的车辆信息列表
     */

    @PreAuthorize("@ss.hasPermi('lh:carrequest:listno')")
    @GetMapping("/nolist")
    public TableDataInfo listno(LhCarrequest lhCarrequest) {
        startPage();
        //List<SysStudent> list = sysStudentService.noFinishTask(getUserId(),"myStudentProcess");//关联工作流没有带条件查询
        List<LhCarrequest> list = lhCarrequestService.noFinishTask(getUserId(), "carrequestProcess", lhCarrequest);//关联工作流带条件查询
        return getDataTable(list);


    }

    /**
     * 获取用车申请信息详细信息
     */
    @GetMapping(value = "/getcarrequestComment/{requestId}")
    public AjaxResult getInfoComment(@PathVariable("requestId") Long requestId) {
        LhCarrequest lhCarrequest = lhCarrequestService.selectLhCarrequestByRequestId(requestId);

        lhCarrequest.setTaskCommentVoList(lhCarrequestService.getTaskCommentList(lhCarrequest.getFlowInstantId()));  //找出前面审批人的审批信息

        return success(lhCarrequest);
    }

    /**
     * 查询未审批的学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('lh:carrequest:editnoapprove')")
    @Log(title = "查询未审批的车辆申请信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/editapprove")
    public AjaxResult editSaveNoAuthData(@RequestBody LhCarrequest lhCarrequest) {
        lhCarrequestService.completeTask(lhCarrequest.getFlowInstantId(), lhCarrequest.getAgree(), getUserId(), lhCarrequest.getComment(), lhCarrequest.getRequestId());
        return toAjax(200);
    }


    /**
     * 查询完成审批的车辆信息列表
     */

    @PreAuthorize("@ss.hasPermi('lh:carrequest:listFinished')")
    @GetMapping("/listFinished")
    public TableDataInfo listFinished(LhCarrequest lhCarrequest) {
        startPage();
        //List<SysStudent> list = sysStudentService.finishTask(getUserId(),"myStudentProcess");//关联工作流没有带条件查询
        List<LhCarrequest> list = lhCarrequestService.finishTask(getUserId(), "carrequestProcess", lhCarrequest);//关联工作流带条件查询
        return getDataTable(list);
    }


    @GetMapping("/listreport")
    public List<ReportBarchartVo> reportBarchartVos(){
        return lhCarrequestService.reportcount();
    }


}
